
import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home/index.vue'
import { useUserStore } from '../stores/user'
import { hasAnyPermission } from '../utils/permission' // 添加权限方法导入

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('../views/About/index.vue')
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('../views/404.vue'),
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  const requiredPermissions = to.meta?.permissions || []
  
  if (requiredPermissions.length && !hasAnyPermission(userStore.user, requiredPermissions)) {
    next({ path: '/404' })
  } else {
    next()
  }
})

export default router
